<?php


namespace app\admin\controller;

use app\admin\validate\FreightValidate;
use app\admin\validate\IDMustBePositiveInt;
use app\admin\validate\UserValidate;
use app\service\model\FreightModel;
use app\service\Y;
use think\Request;

class Freight extends BaseAdmin
{

    /**
     * 运费列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index(Request $request): \think\response\Json
    {
            $where = [];
            $data = FreightModel::where($where)->paginate([
                'list_rows'=> input('param.limit', 10),
                'query' => input('param.')
            ])->toArray();
            return Y::table($data['data'], $data['total']);
    }

    /**
     * 创建、编辑运费
     * @param Request $request
     * @param UserValidate $user_validate
     * @return \think\response\Json
     * @throws \app\service\exception\ParameterException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request, FreightValidate $freight_validate): \think\response\Json
    {
        $id = input('param.id', 0);

        if ($request->isPost()) {
            $freight_validate->goCheck();
            $param = $freight_validate->getDataByRule();

            return FreightModel::saveData($param, $id);
        }

        $data = [];
        if ($id > 0) {
            $data = FreightModel::find($id);
        }
        return Y::success($data, '运费数据');
    }

    /**
     * 删除数据
     * @param IDMustBePositiveInt $IDMustBePositiveInt
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(IDMustBePositiveInt $IDMustBePositiveInt): \think\response\Json
    {
        $IDMustBePositiveInt->goCheck();
        $id = input('param.id');
        return $this->deleteDate((new FreightModel()), $id);
    }
}